package com.example.experiment_8;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private Button button;
    private ImageView imageView;
    private boolean isOpen = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();

        // 按钮点击事件
        button.setOnClickListener(view -> {
            if (isOpen) {
                button.setText("开灯");
                button.setBackgroundResource(R.drawable.btn_close);
                imageView.setImageResource(R.drawable.img_close);
                // 关闭服务
                Intent intent = new Intent(MainActivity.this, MyService.class);
                stopService(intent);
            } else {
                button.setText("关灯");
                button.setBackgroundResource(R.drawable.btn_open);
                imageView.setImageResource(R.drawable.img_open);
                // 开启服务
                Intent intent = new Intent(MainActivity.this, MyService.class);
                startService(intent);
            }
            isOpen = !isOpen;
        });
    }

    private void initView() {
        button = findViewById(R.id.button);
        imageView = findViewById(R.id.image_view);
    }

}